package GUI;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;
/*
import test.PeopleModel;
import test.Person;
import test.PeopleModel.Columns;
*/
import ControlLayer.StorageCtr;
import ModelLayer.Item;
import ModelLayer.StorageContainer;

public class CustomItemModel extends AbstractTableModel {

private static final long serialVersionUID = 1L;
private StorageCtr storageCtr;


private enum Columns {
	/*
	 *  this.itemCode = itemCode;
         this.name = name;
         this.type = type;
         this.price = price;
         this.discount = discount;
	 */
ITEMCODE("Varekode", String.class),
NAME("Navn", String.class),
TYPE("Varetype", String.class),
PRICE("Pris", Double.class),
DISCOUNT("Rabat", Double.class);



private final String title;
private final Class<?> cls;



private Columns(String columnTitle, Class<?> displayClass) {
title = columnTitle;
cls = displayClass;
}



public String getTitle() {
return title;
}



public Class<?> getDisplayClass() {
return cls;
}
};



//private final List<Person> people = new ArrayList<Person>();
private final List<Item> items = StorageContainer.getInstance().getItemsArrayList();


public void addItem(Item i) {
	items.add(i);
	fireTableRowsInserted(items.size() - 1, items.size() - 1);
}

public void removeItem(int index) {
			if(index != -1)
			{	
				items.remove(index);
				fireTableRowsDeleted(index, index);
			}	
		
				
		
		
			
		
}

public void updateItem(int index, String itemCode, String itemName) {
	
	// mangler noget
	items.get(index).setItemCode(itemCode);
	items.get(index).setName(itemName);
	fireTableRowsUpdated(index, index);
	
}

public List<Item> getItems() {
return new ArrayList(items);
}



public int getRowCount() {
return items.size();
}



public int getColumnCount() {
	return Columns.values().length;
}



public Object getValueAt(int rowIndex, int columnIndex) {
	

	Item i = items.get(rowIndex);
	


switch (Columns.values()[columnIndex]) {

/*
 * ITEMCODE("Varekode", String.class),
NAME("Navn", String.class),
TYPE("Varetype", String.class),
PRICE("Pris", Double.class),
DISCOUNT("Rabat", Double.class);
 */
case ITEMCODE:
return i.getItemCode();

case NAME:
return i.getName();

case TYPE:
return i.getType();



case PRICE:
return i.getPrice();



case DISCOUNT:
return i.getDiscount();



default:
return "";
}
}



@Override
public String getColumnName(int columnIndex) {
return Columns.values()[columnIndex].getTitle();
}


@Override
public Class<?> getColumnClass(int columnIndex) {
return Columns.values()[columnIndex].getDisplayClass();
}
}